package cn.com.cifi.mars.util;

import cn.com.cifi.mars.entity.PriceApprovalDifferencesBuild;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.usermodel.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * 类名称：PriceApprovalDifferencesBuildTemplate
 * 类描述：货值审批楼栋均价差异表
 *
 * @author yanboliu
 * @version 1.0.0
 */
@Slf4j
public class PriceApprovalDifferencesBuildTemplate {
    /**
     * 表格模板地址
     */
    private static final String TEMPLATE_PATH = "/template/price_approval_differences_build_template.xlsx";
    /**
     * 初始化文件
     */
    private XSSFWorkbook workbook;
    /**
     * 初始化表格
     */
    private XSSFSheet sheet;
    /**
     * 默认sheet索引
     */
    private int defaultSheetIndex = 0;
    /**
     * 数据开始行
     */
    private int dataBeginRow = 2;
    /**
     * 当前行标
     */
    private int currentDataRowCursor = dataBeginRow;
    /**
     * 单元格样式
     */
    private XSSFCellStyle cellStyle;
    /**
     * 文字样式1
     */
    private XSSFCellStyle cellStyle1;
    /**
     * 文字样式2
     */
    private XSSFCellStyle cellStyle2;
    /**
     * 文字样式3
     */
    private XSSFCellStyle cellStyle3;

    /**
     * 设置表格基本属性
     */
    public PriceApprovalDifferencesBuildTemplate() {

        try {
            InputStream tempInputStream = PriceApprovalDifferencesBuildTemplate.class.getResourceAsStream(TEMPLATE_PATH);
            if (tempInputStream != null) {
                workbook = new XSSFWorkbook(tempInputStream);
                sheet = workbook.getSheetAt(defaultSheetIndex);
                XSSFRow row = sheet.getRow(dataBeginRow);
                cellStyle1 = sheet.getRow(2).getCell(0).getCellStyle();
                cellStyle2 = sheet.getRow(2).getCell(1).getCellStyle();
                cellStyle3 = sheet.getRow(2).getCell(2).getCellStyle();
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量插入行
     *
     * @param differencesBuilds
     * @param sheetIndex
     */
    public synchronized void appendRows(List<PriceApprovalDifferencesBuild> differencesBuilds, int sheetIndex) {
        sheet = workbook.getSheetAt(sheetIndex);
        differencesBuilds.forEach(entity -> {
            this.appendRow(entity);
        });

    }

    /**
     * 插入行
     *
     * @param build
     */
    public synchronized void appendRow(PriceApprovalDifferencesBuild build) {
        int index = 0;
        String defaultValue = "null";
        String divisionSign = "%";

        XSSFRow row = sheet.getRow(currentDataRowCursor);
        if (row == null) {
            row = sheet.createRow(currentDataRowCursor);
        }
        //项目信息
        cellStyle = cellStyle1;
        this.appendCell(row, index++, build.getObjName());
        cellStyle = cellStyle2;
        if (StringUtils.isEmpty(build.getDivergeRate()) || StringUtils.equalsIgnoreCase(defaultValue, build.getDivergeRate())) {
            this.appendCell(row, index++, defaultValue);
        } else {
            this.appendCell(row, index++, String.format("%.2f", Double.parseDouble(build.getDivergeRate()) * 100).concat(divisionSign));
        }
        cellStyle=cellStyle3;
        this.appendCell(row, index++, modifyMathAround(build.getStrategyAvg()));
        this.appendCell(row, index++, modifyMathAround(build.getPresidentAvg()));
        this.appendCell(row, index++, modifyMathAround(build.getBeforeAvg()));
        this.appendCell(row, index++, modifyMathAround(build.getAfterAvg()));
        this.appendCell(row, index++, modifyMathAround(build.getAfterStrategy()));
        this.appendCell(row, index++, modifyMathAround(build.getAfterPresident()));
        this.appendCell(row, index++, modifyMathAround(build.getAfterBfore()));
        currentDataRowCursor++;
    }

    /**
     * 编辑数值四舍五入
     *
     * @param value
     * @return
     */
    private String modifyMathAround(String value) {
        String defaultValue = "null";
        if (StringUtils.equalsIgnoreCase(defaultValue, value) || StringUtils.isEmpty(value)) {
            value = defaultValue;
        } else {
            value = String.valueOf(Math.round(Double.parseDouble(value)));
        }
        return value;
    }

    /**
     * 插入列
     *
     * @param row
     * @param index
     * @param value
     */
    public synchronized void appendCell(XSSFRow row, int index, String value) {
        String defaultValue = "null";
        String divisionSign = "%";
        XSSFCell cell = row.getCell(index);
        if (cell == null) {
            cell = row.createCell(index);
        }
        cell.setCellStyle(cellStyle);
        if (StringUtils.isNotBlank(value) && value.endsWith(divisionSign)) {
            cell.setCellValue(value == null || StringUtils.equals(value, defaultValue) ? " " : value);
        } else {
            ReportUtils.setCellValue(cell, value == null || StringUtils.equals(value, defaultValue) ? " " : value);
        }
    }

    /**
     * 导出
     *
     * @param outputStream
     * @throws IOException
     */
    public void write(OutputStream outputStream) throws IOException {
        workbook.write(outputStream);
    }
}
